package  
{
	import as3isolib.geom.Pt;
	import com.smartfoxserver.v2.entities.Room;
	import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
	import com.smartfoxserver.v2.requests.CreateRoomRequest;
	import com.smartfoxserver.v2.requests.JoinRoomRequest;
	import com.smartfoxserver.v2.requests.LeaveRoomRequest;
	import com.smartfoxserver.v2.requests.RoomSettings;
	import com.smartfoxserver.v2.requests.SetRoomVariablesRequest;
	import com.smartfoxserver.v2.SmartFox;
	import com.smartfoxserver.v2.entities.data.SFSObject;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	
	import UI.*;
	
	/**
	 * ...
	 * @author Bryan Keiren & Sander van Katwijk
	 */		
	public class ButtonFunctions 
	{
		
		public function ButtonFunctions() 
		{
			
		}

		static public function LogIn(data:Object):void
		{
			if (!MessageHandler.Get().m_SFS.isConnected)
			{
				MessageHandler.Get().m_SFS.connect();
			}
			
			var textfield:TextField = data as TextField;
			var username:String = "";
			
			if (textfield.text != "")
			{
				username = textfield.text;
			}
			else
			{
				username = "User#" + Math.round(Math.random() * 1000000);
			}
			
			LocalPlayer.Get().setName(username);
			MessageHandler.Get().sendLoginRequest(username, "", Globals.Zone_Name);
		}
		
		static public function JoinGame(data:Object):void
		{
			if (Globals.Selected_Game_Bool)
			{
				/*
				var sfsobj:SFSObject = new SFSObject();
				sfsobj.putUtfString("gamename", Globals.Selected_Game_Name);
			
				MessageHandler.Get().sendExtensionRequest("entersession", sfsobj);
				*/
			
				MessageHandler.Get().sendJoinRoomRequest(Globals.Selected_Game_Name);
			}
			else
			{
				Engine.printf("No session selected!");
			}
			//Engine.Get().SwitchAppState(AppState.GAME);
		}
		
		static public function SetSelectedGame(data:Object):void
		{
			Globals.Selected_Game_Name = data as String;	// The data passed to this function is assumed to be of type String.
			Globals.Selected_Game_Bool = true;
			
			Engine.Get().m_SelectedGameTextField.text = "> Selected Session:\n  " + Globals.Selected_Game_Name;
			Engine.Get().m_SelectedGameTextField.setTextFormat(Globals.Text_Format_Consolas20_AlignLeft);
			
			var JoinSessionBtn:Button = Engine.Get().m_Menu.getPage(1).getButton("JoinSession");
			if (JoinSessionBtn)
			{
				JoinSessionBtn.activate();
			}
		}
		
		static public function NewGame(data:Object):void
		{
			/*
			MessageHandler.Get().sendExtensionRequest("newgame", new SFSObject());
			*/
			
			var settings:RoomSettings = new RoomSettings("Session: " + LocalPlayer.Get().getName() + "'s Game");
			settings.maxSpectators = 0;
			settings.maxUsers = 4;
			settings.isGame = true;	// The creator joins this room automatically.
			
			var numNodes:int = 5;
			settings.maxVariables = 50;
			settings.variables = new Array();
			settings.variables.push( new SFSRoomVariable("numnodes", numNodes) );
			settings.variables.push( new SFSRoomVariable("gamestate", GameState.Pregame) );	// Gamestate is an integer representing:
																							// 0: Game hasn't begun yet,
																							// 1: Game is currently in progress.
																							// 2: Game has ended.
			
			var flagLocation:Pt;
			for (var i:int = 0; i < numNodes; ++i)
			{
				if (i < Globals.initialFlagLocations.length)
				{
					flagLocation = Globals.initialFlagLocations[i];
				}
				else
				{
					flagLocation = new Pt(Math.random() * Globals.Grid_Size, Math.random() * Globals.Grid_Size);
				}
				settings.variables.push( new SFSRoomVariable("node" + (i + 1) + "x", flagLocation.x) );
				settings.variables.push( new SFSRoomVariable("node" + (i + 1) + "y", flagLocation.y) );
				settings.variables.push( new SFSRoomVariable("node" + (i + 1) + "owner", 0) );
			}
			
			MessageHandler.Get().m_SFS.send(new CreateRoomRequest(settings, true));
		}
		
		static public function ExitGame(data:Object):void
		{
			/*
			MessageHandler.Get().sendExtensionRequest("exitsession", new SFSObject());
			*/
			
			MessageHandler.Get().m_SFS.send(new LeaveRoomRequest(Globals.Current_Game_Room));
			
			Engine.Get().SwitchAppState(AppState.LOBBY);
		}
		
		static public function RefreshGameList(data:Object):void
		{
			/*
			MessageHandler.Get().sendExtensionRequest("gameslist", new SFSObject());
			*/
						
			Engine.Get().m_SelectedGameTextField.text = "> No session selected";
			Engine.Get().m_SelectedGameTextField.setTextFormat(Globals.Text_Format_Consolas20_AlignLeft);
			Globals.Selected_Game_Name = "";	// The data passed to this function is assumed to be of type String.
			Globals.Selected_Game_Bool = false;
			var JoinSessionBtn:Button = Engine.Get().m_Menu.getPage(1).getButton("JoinSession");
			if (JoinSessionBtn)
			{
				JoinSessionBtn.deactivate();
			}
			
			Engine.Get().updateSessionList();
		}
		
		static public function LogInOnEnterKey(evt:KeyboardEvent):void
		{
			Globals.UserName_InputField.setTextFormat(Globals.Text_Format_Consolas25_Centered);
			if (evt.keyCode == 13)	// Enter key.
			{
				LogIn(Globals.UserName_InputField);
			}
		}
	}

}